home *** CD-ROM | disk | FTP | other *** search
/ PCMania 73 / PCMania CD73_1.iso / pcmania / demosc73 / BP70BUG / UTIL.ZIP / COUNT.ASM < prev    next >
Assembly Source File  |  1996-12-16  |  2KB  |  77 lines

  1. ; TUBRO Pascal version 4.0   EXTERNAL FUNCTION        89/05/15 ...red }
  2. ; =================================================================== }
  3. ; Copyright 1996 Roger E. Donais              <rdonais@southeast.net> }
  4. ; May be used freely as long as due credit is given                   }
  5. ; ------------------------------------------------------------------- }
  6.  
  7. ; FUNCTION Count(SubString,MainString: String): Integer;
  8. ; { =================================================================== }
  9. ; { Returns the count of the number of occurances of "SubString" in     }
  10. ; { "MainString".                                                       }
  11. ; { =================================================================== }
  12.  
  13. CODE     SEGMENT   BYTE
  14. ASSUME   CS:CODE,DS:CODE,ES:CODE,SS:CODE
  15.  
  16.          PUBLIC Count,Dcount
  17. PROCESS  PROC FAR
  18.  
  19. MainStr = DWORD PTR BP+8
  20. Fence   = DWORD PTR BP+12
  21.  
  22. COUNT   LABEL FAR
  23.          db 0Ch    ; Clear carry   (or AL,imm8)
  24.  
  25. DCOUNT  LABEL FAR
  26.          stc       ; Set carry     (1-Byte opcode)
  27.  
  28.          push   DS
  29.          push   BP
  30.          mov    BP,SP
  31.  
  32.          mov    AX, 0            ; preserve carry flag
  33.  
  34.          cld
  35.          lds    SI,[Fence]
  36.          les    DI,[MainStr]
  37.          mov    CL,ES:[DI]
  38.          mov    CH,0
  39.          jcxz   @@2              ; Null MainStr returns 0 Count, 0 Field
  40.  
  41.          pushf
  42.          mov    DX,CX
  43.          mov    CL,[SI]
  44.          jcxz   @@3              ; No SubStr retuns 0 Count, or 1 Field
  45.  
  46.          sub    DX,CX
  47.          jb     @@3              ; No match retuns 0 Count, or 1 Field
  48.  
  49.          inc    SI
  50.  
  51. @@1:     inc    DI
  52.          push   CX
  53.          push   SI
  54.          push   DI
  55.          rep    CMPSB
  56.          pop    DI
  57.          pop    SI
  58.          pop    CX
  59.          jne    @@4
  60.  
  61.          inc    AX               ; Increment occurance
  62.  
  63. @@4:     dec    DX
  64.          jns    @@1
  65.  
  66. @@3:     popf                    ; DCOUNT returns number of fields
  67.          adc    AX,0             ; Count  returns number of occurances
  68.  
  69. @@2:     pop    BP
  70.          pop    DS
  71. exit:    retf   8
  72.  
  73. PROCESS  ENDP
  74. CODE     ENDS
  75.          END
  76.  
  77.